<template>
  <div>
    <el-dialog title="新增订阅配置" :visible.sync="dialogVisible" width="510px" :before-close="closeAddDialog"
      :close-on-click-modal="false">
      <el-form :model="addForm" :rules="rules" ref="refAddForm" label-width="100px">
        <!-- <el-form-item size="small">
          <el-radio v-model="radio" label="1">单个新增</el-radio>
          <el-radio v-model="radio" label="2" style="margin-left: 20px"
            >批量新增</el-radio
          >
        </el-form-item> -->

        <el-form-item size="small" label="组织机构" prop="parentId">
          <TreeSearchAdd style="width: 100%" @changeOrgId="changeOrgId" @onClear="onClear" ref="refTreeSearchAdd">
          </TreeSearchAdd>
        </el-form-item>

        <el-form-item size="small" label="联网单位" prop="targetId">
          <!-- <el-select size="small" style="width: 100%" v-loadMore="loadMore" v-model="addForm.targetId"
            placeholder="请选择单位" multiple collapse-tags>
            <el-option v-for="item in orgList" :label="item.orgName" :value="item.id" :key="item.id">
            </el-option>
            <div class="div_box" v-loading="loading" :style="loading ? '' : 'display: none;'"></div>
          </el-select> -->

          <el-select size="small" style="width: 100%;" clearable v-loadMore="unitMore" v-model='addForm.targetId'
            placeholder='请选择' filterable remote :remote-method="remoteMethod" @clear="handleClear">
            <el-option v-for='item in orgList' :label='item.orgName' :value='item.id' :key='item.id'>
            </el-option>
            <div class="div_box" v-loading="unitloading" :style="unitloading ? '' : 'display: none;'"> </div>
          </el-select>

        </el-form-item>

        <el-form-item size="small" label="事件类型" prop="event">
          <el-select v-model="addForm.event" multiple placeholder="请选择" style="width: 100%">
            <el-option v-for="item in eventList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item size="small" label="URL" prop="url">
          <el-input clearable v-model.trim="addForm.url" placeholder="请输入" maxlength="100" style="width: 100%">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeAddDialog" class="cancel-button">取 消</el-button>
        <el-button size="small" @click="addSubmit" v-preventReClick="1000" class="confirm-button">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqSubscribeadd, reqOrgList, reqSubscribebatchAdd } from "@api/api";
import { mapState } from "vuex";
import loadMore from "@mixin/loadMore";
import TreeSearchAdd from "@components/TreeSearchAdd.vue";

export default {
  props: ["organParams"],
  mixins: [loadMore],
  components: { TreeSearchAdd },
  data() {
    return {
      dialogVisible: false,
      radio: "1",
      addForm: {
        targetId: "",
        targetId2: "",
        url: "",
        event: "",
        parentId: "",
      },
      eventList: [
        {
          id: 1,
          name: "设备状态更新",
        },
        {
          id: 2,
          name: "告警事件",
        },
        {
          id: 3,
          name: "绑定事件",
        },
        {
          id: 4,
          name: "解绑事件",
        },
      ],
      // 表单验证规则
      rules: {
        url: [
          {
            required: true,
            message: "请输入URL",
            trigger: "blur",
          },
        ],
        parentId: [
          {
            required: true,
            message: "请选择组织机构",
            trigger: ["blur", "change"],
          },
        ],
        targetId: [
          {
            required: true,
            message: "请选择联网单位",
            trigger: ["blur", "change"],
          },
        ],
        targetId2: [
          {
            required: true,
            message: "请选择组织机构",
            trigger: ["blur", "change"],
          },
        ],
        event: [
          {
            required: true,
            message: "请选择事件类型",
            trigger: ["blur", "change"],
          },
        ],
      },

      orgList: [], //组织机构数据
      sysInstanceIndex: 1,
      sysInstanceTotalPages: 10,
      loading: false,

      unitIndex: 1,
      unitPages: 10,
      unitloading: false,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {
    dialogVisible(e) {
      if (e) {
        // console.log(this.addForm);
        this.$nextTick(() => {
          this.closeAddDialog2();
          this.$refs.refTreeSearchAdd.getDataList();
          this.$refs.refTreeSearchAdd.onclose();
        });
      }
    },
    // radio(e) {
    //   this.addForm.targetId = "";
    // },
  },
  methods: {
    changeOrgId(instanceId) {
      this.addForm.parentId = instanceId.id;
      this.handleClear();
    },
    onClear() {
      this.addForm.parentId = ''
      this.handleClear();
    },
    // 清空
    handleClear() {
      this.orgName = '';
      this.unitIndex = 1;
      this.unitPages = 10;
      this.orgList = []
      this.addForm.targetId = "";
      this.getOrgList()
    },
    // 远程搜索
    async remoteMethod(query) {
      this.addForm.targetId = query //保留输入的值
      this.orgName = query;
      this.unitIndex = 1;
      this.unitPages = 10;
      this.orgList = []
      this.getOrgList()
    },
    async unitMore() {
      if (this.unitIndex >= Math.ceil(this.unitPages / 10)) return
      this.unitIndex++
      this.unitloading = true;
      // 获取列表数据的方法
      await this.getOrgList()
      this.unitloading = false;
    },
    // 请求单位列表
    async getOrgList() {
      let { tenantId, userId, roleName } = this.user;
      const res = await reqOrgList({
        tenantId,
        userId,
        roleName,
        orgName: this.orgName,
        parentId: this.addForm.parentId,
        type: "4"
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.orgList = res.data.rows
    },
    // 点击新增
    addSubmit() {
      this.$refs.refAddForm.validate(async (valid) => {
        if (!valid) return;
        if (this.radio == "1") {
          const res = await reqSubscribeadd({
            url: this.addForm.url,
            event: this.addForm.event,
            targetId: this.addForm.targetId,
            tenantId: this.user.tenantId,
          });
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        } else {
          const res = await reqSubscribebatchAdd({
            url: this.addForm.url,
            event: this.addForm.event,
            // targetIdList: this.addForm.targetId,
            targetId: this.addForm.targetId,
            tenantId: this.user.tenantId,
          });
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        }

        this.$modal.notifySuccess("新增订阅配置成功");
        this.closeAddDialog();
        this.$parent.getDataList();
      });
    },
    // 点击关闭弹框
    closeAddDialog() {
      this.$refs.refAddForm.resetFields();
      this.dialogVisible = false;
      this.orgList = []; //组织机构数据
      this.addForm.targetId = "";
      this.sysInstanceIndex = 1;
      this.sysInstanceTotalPages = 10;
      this.radio = "1";
    },
    closeAddDialog2() {
      this.$refs.refAddForm.resetFields();
    },
  },
  mounted() { },
  deactivated() {
    if (this.$refs.refAddForm != null) {
      this.closeAddDialog();
    }
  },
};
</script>

<style lang="less" scoped>
@import url("../../../common.less");

// 批量按钮开始
::v-deep .el-radio {
  color: @primaryThemeTxtColor2;
}

::v-deep .el-radio__input.is-checked+.el-radio__label {
  color: #7367f0;
}

::v-deep .el-radio__input.is-checked .el-radio__inner {
  border-color: #7367f0;
  background: #7367f0;
}

// 批量按钮结束

.orgNameclass {
  color: @primaryThemeTxtColor2;
}
</style>
